package com.njmd.mdbs.web.security;

import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

import com.njmd.mdbs.web.entity.sys.SysLogin;

/**
 * <p>
 * User: Zhang Kaitao
 * <p>
 * Date: 14-1-28
 * <p>
 * Version: 1.0
 */
public class PasswordHelper
{

	private RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
	private String algorithmName = "md5";
	private int hashIterations = 2;

	public void setRandomNumberGenerator(RandomNumberGenerator randomNumberGenerator)
	{
		this.randomNumberGenerator = randomNumberGenerator;
	}

	public void setAlgorithmName(String algorithmName)
	{
		this.algorithmName = algorithmName;
	}

	public void setHashIterations(int hashIterations)
	{
		this.hashIterations = hashIterations;
	}

	public void encryptPassword(SysLogin user)
	{
		user.setLoginPwdSalt(randomNumberGenerator.nextBytes().toHex());
		String newPassword = new SimpleHash(algorithmName, user.getLoginPwd(), ByteSource.Util.bytes(user.getCredentialsSalt()), hashIterations).toHex();
		user.setLoginPwd(newPassword);
	}
}
